package com.bp.hearthealth.pulse.lnapps.bp_view

import android.content.Context
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.drawable.GradientDrawable
import android.util.AttributeSet
import android.view.View
import com.bp.hearthealth.pulse.lnapps.R
import com.bp.hearthealth.pulse.lnapps.R.styleable.GradientView
import com.bp.hearthealth.pulse.lnapps.bp_extension.UIHelperBp


class BPCustomGradientView : View {
    private val gradientColors = intArrayOf(
        Color.parseColor("#027FFB"),
        Color.parseColor("#2BBC8B"),
        Color.parseColor("#FFB900"),
        Color.parseColor("#F64A10")
    )

    private var progressNum = 0//心率值
    private var textPaint: Paint? = null
    private var gradientDrawable: GradientDrawable? = null
    private var shouldDraw = false

    constructor(context: Context?) : super(context) {
//        init()
    }

    constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs) {
        initAttrs(attrs)
//        init()
    }

    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context, attrs, defStyleAttr
    ) {
        initAttrs(attrs)
//        init()
    }

    private fun initAttrs(attrs: AttributeSet?) {
        attrs?.let {
            val context = context
            val typedArray = context.obtainStyledAttributes(attrs, GradientView)
            progressNum = typedArray.getInteger(R.styleable.GradientView_gvProgressNum, 0)
            typedArray.recycle()
        }
    }

    private fun init() {
        // 创建GradientDrawable对象
        gradientDrawable = GradientDrawable(GradientDrawable.Orientation.LEFT_RIGHT, gradientColors)

        // 设置圆角半径为10dp
        val cornerRadius = resources.displayMetrics.density * 10
        gradientDrawable!!.cornerRadius = cornerRadius

        // 设置View的背景为GradientDrawable
        background = gradientDrawable

        // 初始化绘制文字的Paint
        textPaint = Paint()
        textPaint!!.color = UIHelperBp.getColor(R.color.color_666666)
        textPaint!!.textSize = resources.displayMetrics.density * 12
        textPaint!!.textAlign = Paint.Align.CENTER
    }

    fun setProgressNum(progress: Int) {
        this.progressNum = progress
        shouldDraw = true
//        invalidate()
        init()
    }

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        if (shouldDraw) {
            val heightInPixels = (resources.displayMetrics.density * 20).toInt()
            gradientDrawable!!.setBounds(0, getSize(9), width, heightInPixels)
            gradientDrawable!!.draw(canvas)
            // 绘制底部文字
            drawBottomText(canvas)
            // 在GradientDrawable的顶部添加一个本地图片
            if (progressNum < 60) {
                drawImageOnTop(canvas, R.mipmap.heart_rate_low_arrow_bp)
            } else if (progressNum in 60..100) {
                drawImageOnTop(canvas, R.mipmap.heart_rate_normal_arrow_bp)
            } else {
                drawImageOnTop(canvas, R.mipmap.heart_rate_high_arrow_bp)
            }
        }
    }

    private fun drawBottomText(canvas: Canvas) {
        val width = width
        val numberOfParts = 6 // 将View分为6份
        // 数字数组
        val numbers = arrayOf("60", "70", "80", "90", "100")
        for (i in numbers.indices) {
            val x = (i + 1) * (width.toFloat() / numberOfParts)
            val y = height - textPaint!!.fontMetrics.bottom
            canvas.drawText(numbers[i], x, y, textPaint!!)
        }
    }

    private fun drawImageOnTop(canvas: Canvas, imageResId: Int) {
//        val numbers = arrayOf("60", "75", "80", "100", "110")
//        for (i in numbers.indices) {
//            val bitmap = BitmapFactory.decodeResource(resources, imageResId)
//            val x = calculateImagePosition(numbers[i].toInt())
//            val y = 0f
//            val imageWidth = getSize(18)
//            val imageHeight = getSize(18)
//            canvas.drawBitmap(
//                Bitmap.createScaledBitmap(bitmap, imageWidth, imageHeight, false), x, y, null
//            )
//        }
        val bitmap = BitmapFactory.decodeResource(resources, imageResId)
        val x = calculateImagePosition(progressNum)
        val y = 0f
        val imageWidth = getSize(18)
        val imageHeight = getSize(18)
        canvas.drawBitmap(
            Bitmap.createScaledBitmap(bitmap, imageWidth, imageHeight, false), x, y, null
        )
//        LogUtils.i("总长度为: $width")
    }

    private fun calculateImagePosition(position: Int): Float {
        val width = width
        val numberOfParts = 6 // 将View分为6份
        val i = width.toFloat() / numberOfParts
//        LogUtils.i("单个尺寸: $i")
        if (position < 60) {
            return i / 60 * position - getTextWidth(textPaint, position.toString()) / 2f
        }
        if (position in 100..200) {
            return i * 5 + i / 101 * (101 - (201 - position)).toFloat() - getTextWidth(
                textPaint, position.toString()
            ) / 2f
        }
        if (position < 100) {
            var i1 = (width - 2 * i) / 41 * (41 - (101 - position))
//            if (position > 85) {
//                i1 = (width - 2 * i) / 39 * (39 - (100 - position))
//            }
            return i + i1 - getTextWidth(textPaint, position.toString()) / 2f
        }
        return position * (width.toFloat() / numberOfParts)
    }

    private fun getTextWidth(paint: Paint?, text: String): Int {
        return paint!!.measureText(text).toInt()
    }

    private fun getSize(size: Int): Int {
        return dpToPx(size.toFloat()).toInt()
    }

    private fun dpToPx(dp: Float): Float {
        return dp * resources.displayMetrics.density
    }
}

